/* Jimmy Manone
 * Lab 05 T01
 * 10/14/2014
 */


#include	<stdint.h>
#include	<stdbool.h>
#include	"inc/hw_memmap.h"
#include	"inc/hw_types.h"
#include	"driverlib/debug.h"
#include	"driverlib/sysctl.h"
#include	"driverlib/adc.h" //Definitions for the ADC driver
#define     TARGET_IS_BLIZZARD_RB1 //Internal TI product name
#include    "driverlib/rom.h" //Access the API's ROM

#ifdef	DEBUG //Eliminates potential driver errors and library conflicts
void__error__(char *pcFilename,	uint32_t ui32Line)
{
}
#endif



int main(void) {
	
	uint32_t ui32ADC0Value[4]; //Stores data read in from ADC FIFO

	volatile	uint32_t	ui32TempAvg; //Sensor data variable
	volatile	uint32_t	ui32TempValueC; //Degrees in Celsius
	volatile	uint32_t	ui32TempValueF; //Degrees in Fahrenheit
	//(All volatile for availability at runtime)


	ROM_SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
	//System clock at 40MHz

	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); //Enable ADC0 peripheral

	ROM_ADCHardwareOversampleConfigure(ADC0_BASE, 64);//64 measurements to be averaged together


	ROM_ADCSequenceConfigure(ADC0_BASE,	1,	ADC_TRIGGER_PROCESSOR,	0);
	//ADC sequencer config - sequence triggered by processor, priority=highest

	ROM_ADCSequenceStepConfigure(ADC0_BASE,	1,	0,	ADC_CTL_TS);
	ROM_ADCSequenceStepConfigure(ADC0_BASE,	1,	1,	ADC_CTL_TS);
	ROM_ADCSequenceStepConfigure(ADC0_BASE,	1,	2,	ADC_CTL_TS);
	//Average all 4 samples of temperature sensor data/configure steps 0 to 2

	ROM_ADCSequenceStepConfigure(ADC0_BASE,1,3,ADC_CTL_TS|ADC_CTL_IE|ADC_CTL_END);
	//Sample temperature sensor|Configure interrupt flag|Inform ADC logic last conversion on sequencer 1


	ROM_ADCSequenceEnable(ADC0_BASE,1); //enable ADC sequencer 1

	while(1)
	{
		ROM_ADCIntClear(ADC0_BASE, 1); //Clear flag for ADC interrupt
		ROM_ADCProcessorTrigger(ADC0_BASE,	1);//Trigger ADC conversion

		while(!ROM_ADCIntStatus(ADC0_BASE,	1,	false)) //Wait for conversion to be complete
		{
		}

		ROM_ADCSequenceDataGet(ADC0_BASE,	1,	ui32ADC0Value); //Read the FIFO buffer

		ui32TempAvg = (ui32ADC0Value[0] + ui32ADC0Value[1] + ui32ADC0Value[2] + ui32ADC0Value[3] + 2)/4;
		//Calculate average of temperature sensor data

		ui32TempValueC = (1475-((2475*ui32TempAvg))/4096)/10;
		//Convert to Celsius based on formula provided in the datasheet

		ui32TempValueF = ((ui32TempValueC*9)+160)/5;
		//Use values to calculate the Fahrenheit temperature



	}



}
